package com.mall.service.commodity.service;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.commons.common.CommonPage;
import com.mall.commons.exception.CustomException;
import com.mall.feign.clients.UserClient;
import com.mall.model.domain.*;
import com.mall.model.dto.ProductListDto;
import com.mall.model.dto.ProductcreateDto;
import com.mall.service.commodity.mapper.*;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.util.List;

/**
 * @ClassName PmsProductService
 * @Description 商品逻辑层
 * @Author dcq
 * @Date 2023/3/13 9:37
 * @Version 1.0
 */
@Service
public class PmsProductService {
    @Autowired
    private PmsProductVertifyRecordMapper pmsProductVertifyRecordMapper;
    @Autowired
    private UserClient userClient;
    @Autowired
    private PmsProductMapper productMapper;
    @Autowired
    private PmsMemberPriceMapper pmsMemberPriceMapper;
    @Autowired
    private CmsPrefrenceAreaProductRelationMapper cmsPrefrenceAreaProductRelationMapper;
    @Autowired
    private PmsProductLadderMapper pmsProductLadderMapper;
    @Autowired
    private PmsProductAttributeValueMapper pmsProductAttributeValueMapper;
    @Autowired
    private PmsSkuStockMapper pmsSkuStockMapper;
    @Autowired
    private  CmsSubjectProductRelationMapper cmsSubjectProductRelationMapper;
    @Autowired
    private   PmsProductFullReductionMapper pmsProductFullReductionMapper;
    //根据商品名称或货号模糊查询
    public PmsProduct simpleListSei(String keyword) {
        LambdaQueryWrapper<PmsProduct>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(keyword!=null,PmsProduct::getKeywords,keyword);
        PmsProduct pmsProduct = productMapper.selectOne(queryWrapper);
        return pmsProduct;
    }
    //查询商品
    public CommonPage list(Integer pageNum, Integer pageSize, ProductListDto dto) {
        Page<PmsProduct> objectPage = new Page<>(pageNum, pageSize);
        Integer brandId = dto.getBrandId();
        String keyword = dto.getKeyword();
        String productSn = dto.getProductSn();
        Integer productCategoryId = dto.getProductCategoryId();
        Integer publishStatus = dto.getPublishStatus();
        Integer verifyStatus = dto.getVerifyStatus();
        LambdaQueryWrapper<PmsProduct>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(brandId!=null,PmsProduct::getBrandId,brandId);
        queryWrapper.like(keyword!=null,PmsProduct::getName ,keyword);
        queryWrapper.eq(productCategoryId!=null,PmsProduct::getProductCategoryId,productCategoryId);
        queryWrapper.like(productSn!=null,PmsProduct::getProductSn,productSn);
        queryWrapper.eq(publishStatus!=null,PmsProduct::getPublishStatus,publishStatus);
        queryWrapper.eq(verifyStatus!=null,PmsProduct::getVerifyStatus,verifyStatus);
        queryWrapper.eq(PmsProduct::getDeleteStatus,"0");
        Page<PmsProduct> pmsProductPage = productMapper.selectPage(objectPage, queryWrapper);
        CommonPage page = CommonPage.getPage(pmsProductPage);
        return  page;

    }
     //添加商品
    public void createSei(ProductcreateDto dto) {
         //商品信息 实体类
        PmsProduct pmsProduct = new PmsProduct();
        BeanUtils.copyProperties(dto,pmsProduct);
        productMapper.insert(pmsProduct);
        //商品会员价格表
        List<PmsMemberPrice> memberPriceList = dto.getMemberPriceList();
        if (memberPriceList!=null) {
            for (PmsMemberPrice pmsMemberPrice : memberPriceList) {
                pmsMemberPriceMapper.insert(pmsMemberPrice);
            }
        }
        //优选专区和产品关系
        List<CmsPrefrenceAreaProductRelation> prefrenceAreaProductRelationList = dto.getPrefrenceAreaProductRelationList();
        if (prefrenceAreaProductRelationList!=null) {
            for (CmsPrefrenceAreaProductRelation cmsPrefrenceAreaProductRelation : prefrenceAreaProductRelationList) {
                cmsPrefrenceAreaProductRelationMapper.insert(cmsPrefrenceAreaProductRelation);
            }
        }
        //产品阶梯价格
        List<PmsProductLadder> productLadderList = dto.getProductLadderList();
        if (productLadderList!=null) {
            for (PmsProductLadder pmsProductLadder : productLadderList) {
                pmsProductLadderMapper.insert(pmsProductLadder);
            }

        }
        //存储产品参数信息
        List<PmsProductAttributeValue> productAttributeValueList = dto.getProductAttributeValueList();
        if (productAttributeValueList!=null) {
            for (PmsProductAttributeValue pmsProductAttributeValue : productAttributeValueList) {
                pmsProductAttributeValueMapper.insert(pmsProductAttributeValue);
            }

        }
        //sku的库存
        List<PmsSkuStock> skuStockList = dto.getSkuStockList();
        if (skuStockList!=null) {
            for (PmsSkuStock pmsSkuStock : skuStockList) {
                pmsSkuStockMapper.insert(pmsSkuStock);
            }

        }
        //专题商品关系表
        List<CmsSubjectProductRelation> subjectProductRelationList = dto.getSubjectProductRelationList();
        if (subjectProductRelationList!=null) {
            for (CmsSubjectProductRelation cmsSubjectProductRelation : subjectProductRelationList) {
                cmsSubjectProductRelationMapper.insert(cmsSubjectProductRelation);
            }
        }
        //产品满减表
        List<PmsProductFullReduction> productFullReductionList = dto.getProductFullReductionList();
        if (productFullReductionList!=null) {
            for (PmsProductFullReduction pmsProductFullReduction : productFullReductionList) {
                pmsProductFullReductionMapper.insert(pmsProductFullReduction);
            }

        }
    }
   //批量修改删除状态
    public void deleteStatusSei(Integer[] ids, Integer deleteStatus) {
        if (ids!=null) {
            for (Integer id : ids) {
                if ((productMapper.selectById(id).getDeleteStatus()).equals(deleteStatus)){
                    throw new CustomException("操作错误");
                }
                PmsProduct pmsProduct = productMapper.selectById(id);
                pmsProduct.setDeleteStatus(deleteStatus);
                productMapper.updateById(pmsProduct);
            }
        }
    }
   //批量设为新品
    public void newStatusSei(Integer[] ids, Integer newStatus) {
        if (ids!=null) {
            for (Integer id : ids) {
                if ((productMapper.selectById(id).getNewStatus()).equals(newStatus)){
                    throw new CustomException("操作错误");
                }
                PmsProduct pmsProduct = productMapper.selectById(id);
                pmsProduct.setNewStatus(newStatus);
                productMapper.updateById(pmsProduct);
            }
        }
    }
    //批量上下架商品
    public void publishStatusSei(Integer[] ids, Integer publishStatus) {
        if (ids!=null) {
            for (Integer id : ids) {
                if ((productMapper.selectById(id).getPublishStatus()).equals(publishStatus)){
                    throw new CustomException("操作错误");
                }
                PmsProduct pmsProduct = productMapper.selectById(id);
                pmsProduct.setPublishStatus(publishStatus);
                productMapper.updateById(pmsProduct);
            }
        }
    }
   //批量推荐商品
    public void recommendStatusSei(Integer[] ids, Integer recommendStatus) {
        if (ids!=null) {
            for (Integer id : ids) {
                if ((productMapper.selectById(id).getRecommandStatus()).equals(recommendStatus)){
                    throw new CustomException("操作错误");
                }
                PmsProduct pmsProduct = productMapper.selectById(id);
                pmsProduct.setRecommandStatus(recommendStatus);
                productMapper.updateById(pmsProduct);
            }
        }
    }
    //批量修改审核状态
    public void verifyStatusSei(Integer[] ids, Integer verifyStatus, String detail,    String currentId) {
     //判断ids是否为空  为空要返回错误
        if (ids != null) {
            //遍历ids
            for (Integer id : ids) {
                if ((productMapper.selectById(id).getRecommandStatus()).equals(verifyStatus)) {
                    throw new CustomException("操作错误");
                }
                //找出以前数据  改变状态 重新存入
                PmsProduct pmsProduct = productMapper.selectById(id);
                pmsProduct.setRecommandStatus(verifyStatus);
                productMapper.updateById(pmsProduct);
                PmsProductVertifyRecord productVertify = new PmsProductVertifyRecord();
                //审核数据存入
                UmsAdmin admin = userClient.getId(currentId);
                productVertify.setProductId(Convert.toLong(id));
                productVertify.setVertifyMan(admin.getUsername());
                productVertify.setStatus(verifyStatus);
                productVertify.setDetail(detail);
                pmsProductVertifyRecordMapper.insert(productVertify);
            }
        }

    }
     //更新商品
    public void updateSei(ProductcreateDto dto, Integer id) {
        PmsProduct pmsProduct = new PmsProduct();
        pmsProduct.setId(Convert.toLong(id));
        BeanUtils.copyProperties(dto, pmsProduct);
        productMapper.updateById(pmsProduct);
        //删除商品对应的各表信息 再将最新数据传入
        //商品会员价格表
        List<PmsMemberPrice> memberPriceList = dto.getMemberPriceList();
        if (memberPriceList!=null) {
            LambdaQueryWrapper<PmsMemberPrice>queryWrapper1=new LambdaQueryWrapper();
            queryWrapper1.eq(PmsMemberPrice::getProductId,id);
            pmsMemberPriceMapper.delete(queryWrapper1);
            for (PmsMemberPrice pmsMemberPrice : memberPriceList) {
                pmsMemberPriceMapper.insert(pmsMemberPrice);
            }
        }
        //优选专区和产品关系
        List<CmsPrefrenceAreaProductRelation> prefrenceAreaProductRelationList = dto.getPrefrenceAreaProductRelationList();
        if (prefrenceAreaProductRelationList!=null) {
            LambdaQueryWrapper<CmsPrefrenceAreaProductRelation>queryWrapper2=new LambdaQueryWrapper();
            queryWrapper2.eq(CmsPrefrenceAreaProductRelation::getProductId,id);
            cmsPrefrenceAreaProductRelationMapper.delete(queryWrapper2);
            for (CmsPrefrenceAreaProductRelation cmsPrefrenceAreaProductRelation : prefrenceAreaProductRelationList) {
                cmsPrefrenceAreaProductRelationMapper.insert(cmsPrefrenceAreaProductRelation);
            }
        }
        //产品阶梯价格
        List<PmsProductLadder> productLadderList = dto.getProductLadderList();
        if (productLadderList!=null) {
            LambdaQueryWrapper<PmsProductLadder>queryWrapper3=new LambdaQueryWrapper();
            queryWrapper3.eq(PmsProductLadder::getProductId,id);
            pmsProductLadderMapper.delete(queryWrapper3);
            for (PmsProductLadder pmsProductLadder : productLadderList) {
                pmsProductLadderMapper.insert(pmsProductLadder);
            }

        }
        //存储产品参数信息
        List<PmsProductAttributeValue> productAttributeValueList = dto.getProductAttributeValueList();
        if (productAttributeValueList!=null) {
            LambdaQueryWrapper<PmsProductAttributeValue>queryWrapper4=new LambdaQueryWrapper();
            queryWrapper4.eq(PmsProductAttributeValue::getProductId,id);
            pmsProductAttributeValueMapper.delete(queryWrapper4);
            for (PmsProductAttributeValue pmsProductAttributeValue : productAttributeValueList) {
                pmsProductAttributeValueMapper.insert(pmsProductAttributeValue);
            }

        }
        //sku的库存
        List<PmsSkuStock> skuStockList = dto.getSkuStockList();
        if (skuStockList!=null) {
            LambdaQueryWrapper<PmsSkuStock>queryWrapper5=new LambdaQueryWrapper();
            queryWrapper5.eq(PmsSkuStock::getProductId,id);
            pmsSkuStockMapper.delete(queryWrapper5);
            for (PmsSkuStock pmsSkuStock : skuStockList) {
                pmsSkuStockMapper.insert(pmsSkuStock);
            }

        }
        //专题商品关系表
        List<CmsSubjectProductRelation> subjectProductRelationList = dto.getSubjectProductRelationList();
        if (subjectProductRelationList!=null) {
            LambdaQueryWrapper<CmsSubjectProductRelation>queryWrapper6=new LambdaQueryWrapper();
            queryWrapper6.eq(CmsSubjectProductRelation::getProductId,id);
            cmsSubjectProductRelationMapper.delete(queryWrapper6);
            for (CmsSubjectProductRelation cmsSubjectProductRelation : subjectProductRelationList) {
                cmsSubjectProductRelationMapper.insert(cmsSubjectProductRelation);
            }
        }
        //产品满减表
        List<PmsProductFullReduction> productFullReductionList = dto.getProductFullReductionList();
        if (productFullReductionList!=null) {
            LambdaQueryWrapper<PmsProductFullReduction>queryWrapper7=new LambdaQueryWrapper();
            queryWrapper7.eq(PmsProductFullReduction::getProductId,id);
            pmsProductFullReductionMapper.delete(queryWrapper7);
            for (PmsProductFullReduction pmsProductFullReduction : productFullReductionList) {
                pmsProductFullReductionMapper.insert(pmsProductFullReduction);
            }

        }
    }
    //商品修改回显
    public ProductcreateDto updateIdSei(Integer id) {
        ProductcreateDto dto = new ProductcreateDto();
        PmsProduct pmsProduct = productMapper.selectById(id);
        if (pmsProduct==null){
            throw  new CustomException("商品不存在");
        }
        //优选专区和产品关系
        LambdaQueryWrapper<CmsPrefrenceAreaProductRelation>queryWrapper1C=new LambdaQueryWrapper<>();
        queryWrapper1C.eq(CmsPrefrenceAreaProductRelation::getProductId,pmsProduct.getId());
        List<CmsPrefrenceAreaProductRelation> body1C= cmsPrefrenceAreaProductRelationMapper.selectList(queryWrapper1C);
        dto.setPrefrenceAreaProductRelationList(body1C);
        ////商品会员价格表
        LambdaQueryWrapper<PmsMemberPrice>queryWrapper2P=new LambdaQueryWrapper<>();
        queryWrapper2P.eq(PmsMemberPrice::getProductId,pmsProduct.getId());
        List<PmsMemberPrice> body2P = pmsMemberPriceMapper.selectList(queryWrapper2P);
        dto.setMemberPriceList(body2P);
        //产品阶梯价格
        LambdaQueryWrapper<PmsProductLadder>queryWrapper3P=new LambdaQueryWrapper<>();
        queryWrapper3P.eq(PmsProductLadder::getProductId,pmsProduct.getId());
        List<PmsProductLadder> pmsProductLadders = pmsProductLadderMapper.selectList(queryWrapper3P);
        dto.setProductLadderList(pmsProductLadders);
        // //存储产品参数信息
        LambdaQueryWrapper<PmsProductAttributeValue>queryWrapper4P=new LambdaQueryWrapper<>();
        queryWrapper4P.eq(PmsProductAttributeValue::getProductId,pmsProduct.getId());
        List<PmsProductAttributeValue> body4 = pmsProductAttributeValueMapper.selectList(queryWrapper4P);
        dto.setProductAttributeValueList(body4);
        //sku的库存
        LambdaQueryWrapper<PmsSkuStock>queryWrapper5P=new LambdaQueryWrapper<>();
        queryWrapper5P.eq(PmsSkuStock::getProductId,pmsProduct.getId());
        List<PmsSkuStock> body5 = pmsSkuStockMapper.selectList(queryWrapper5P);
        dto.setSkuStockList(body5);
        // //专题商品关系表
        LambdaQueryWrapper<CmsSubjectProductRelation>queryWrapper6C=new LambdaQueryWrapper<>();
        queryWrapper6C.eq(CmsSubjectProductRelation::getProductId,pmsProduct.getId());
        List<CmsSubjectProductRelation> body6 = cmsSubjectProductRelationMapper.selectList(queryWrapper6C);
        dto.setSubjectProductRelationList(body6);
        //产品满减表

        LambdaQueryWrapper<PmsProductFullReduction>queryWrapper7P=new LambdaQueryWrapper<>();
        queryWrapper7P.eq(PmsProductFullReduction::getProductId,pmsProduct.getId());
        List<PmsProductFullReduction> body7 = pmsProductFullReductionMapper.selectList(queryWrapper7P);
        dto.setProductFullReductionList(body7);
        BeanUtils.copyProperties(pmsProduct,dto);
        return dto ;

    }

    public PmsProduct getId(Integer id) {
        PmsProduct pmsProduct = productMapper.selectById(id);
        return pmsProduct;
    }
}